<?php
$bartUserServices = new BartuserService();
$orderServices = new OrdersService();
$orderDetailService = new OrderDetailsService();
$tool = new Tool();
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);
	if($opt == "POList" && $permit ==true)
	{
		$smarty->assign('tool', $tool);	
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, "2");// 2 is status PO
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderDisplayByTypeByRange($context, $result, "2", $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPurchaseOrder.tpl");
			
	}
	
	if($opt == "POListFact" && $permit ==true)
	{
			
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllPurchaseOrderByFactory($context, $result, $_SESSION['user']['ID']);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllPurchaseOrderByFactoryByRange($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPurchaseOrderF.tpl");
			
	}
	
	if($opt == "searchPOrderFact" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}

		$start=$p->findStart($configPage['limit']);
		$count = $orderServices->countAllByStatusByOrderDateByFactory($context, $result, "2", $dateFrom, $dateTo, $_SESSION['user']['ID']);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->findAllByStatusByOrderDateByRangeByFactory($context, $result, "2", $start, $configPage['limit'], $dateFrom, $dateTo, $_SESSION['user']['ID']);
		
		
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('tool', $tool);
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPurchaseOrderF.tpl");

	}
	
	if($opt == "viewPOList" && $permit ==true)// missing permit
	{
		if(isset($_GET['pid']) && is_numeric($_GET['pid']))
		{
			$purchaseID = $_GET['pid'];
			$purchaseOrder = $orderServices->getOrderByOrderID($context, $result, $purchaseID);
			$start=$p->findStart($configPage['limit']);
			$count=$purchaseOrder->getOrdersDomain()->getFactNum();
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
			$orderList = $orderServices->getPurchaseOrderArr($context, $result, $purchaseID);
			$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			$smarty->assign('tool', $tool);
			$smarty->assign('total', $total);
			$smarty->assign('page_list', $page_list);
			$smarty->assign('orderList', $orderList);
			
			$smarty->display($templateDir . "/temp1/listPurchaseOrderDetails.tpl");
		}
		else
		{
			header("Location:index.php?func=dash");
		}		
	}

	if($opt == "complainF" && $permit ==true)
	{
		$orderID = $_GET['id'];
		$smarty->assign('orderID', $orderID);
			
		$smarty->display($templateDir . "/temp1/complainF.tpl");
	}
	
	if($opt == "searchPOrder" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}

		$start=$p->findStart($configPage['limit']);
		$count = $orderServices->countAllByStatusByOrderDate($context, $result, "2", $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "2", $start, $configPage['limit'], $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('tool', $tool);
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPurchaseOrder.tpl");

	}
	
	
	
	if($opt == 'viewF' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{

			$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
			$orderDetailArr = $order->getOrderDetail();
			
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('order', $order);

			$smarty->display($templateDir . "/temp1/viewOrderF.tpl");
		}
	}
	
	if($opt == 'editPOF' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{

			$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
			$orderDetailArr = $order->getOrderDetail();
			
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('order', $order);

			$smarty->display($templateDir . "/temp1/editPOF.tpl");
		}
	}
	
	if($opt == 'viewPOBArt' && $permit ==true)
	{
		if(isset($_GET['pid']) && is_numeric($_GET['pid']) && isset($_GET['fid']) && is_numeric($_GET['fid']))
		{
			$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['pid'], $_GET['fid']);
			$orderDetailArr = $order->getOrderDetail();
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('tool', $tool);
			$smarty->assign('order', $order);
			$smarty->display($templateDir . "/temp1/viewPOrderBArt.tpl");
		}
	}
}
?>